<?php
	class UserModel extends Model {
		function __construct(){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."user";
			$this->fieldList=array("id","userName", "userPwd", "email", "sex", "regTime","disable","allow_dlht", "allow_fbpl");
		}

	
		function validateForm($status=1){
			$result=true;
			if(!Validate::required($_POST['userName'])) {
				$this->messList[] = "用户名称不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($_POST['userName'], 20)) {
				$this->messList[] = "用户名称的长度不能大于20.";
				$result=false;
			}
			if($status){
				if(!Validate::required($_POST['userPwd'])) {
					$this->messList[] = "用户密码不能为空.";
					$result=false;
				}
			}
			if($_POST['userPwd']!=$_POST['userpwdok']) {
				$this->messList[] = "两次密码输入不一致.";
				$result=false;
			}
			if(!Validate::required($_POST['email'])) {
				$this->messList[] = "用户电子邮件不能为空.";
				$result=false;
			}
			if(!Validate::match($_POST['email'], "/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/")) {
				$this->messList[] = "不是正确的电子邮件格式.";
				$result=false;
			}
	
			return  $result;
		}

		function existsUser($username){
			return $this->total("where userName='{$username}'");
		}

		function login($uname, $pwd) {	
			$sql = "SELECT id FROM {$this->tabName} WHERE userName = '{$uname}' AND userPwd = MD5('{$pwd}')";
		
			$result=$this->mysqli->query($sql);
			if($result && $result->num_rows>0) {	//登录成功
				$data=$result->fetch_assoc();
				$_SESSION['isLoginH'] = true;
				$_SESSION['uid']     = $data['id'];
				$_SESSION['uname']   = $uname;
				return 1;  //登录成功
			
			}else{
				return 0;//登录失败
			}
		}
	
	}
